//
//  HZMarketBottomBarView.m
//  Stock_Health
//
//  Created by MayerF on 2017/5/25.
//  Copyright © 2017年 MayerF. All rights reserved.
//

#import "HZMarketBottomBarView.h"
//#import "ZQHttpManager.h"
@interface HZMarketBottomBarView()
@property (nonatomic, strong)UIButton *pBuyBtn;
@property (nonatomic, strong)UIButton *pSellBtn;
@property (nonatomic, strong)UIButton *pAddOwnBtn;
@end

@implementation HZMarketBottomBarView
{
    NSString *_codeStr;
    NSString *_marketStr;
}
#pragma mark -
#pragma mark init
- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self p_commonInit];
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)p_commonInit
{
    _pBuyBtn = [UIButton new];
    _pSellBtn = [UIButton new];
//    _pAddOwnBtn = [UIButton new];
    
    _pBuyBtn.backgroundColor = kMYHexToRGB(0x00, 1);
    [_pBuyBtn setTitle:@"买入" forState:UIControlStateNormal];
    [_pBuyBtn setTitleColor:kREDColor forState:UIControlStateNormal];
    [_pBuyBtn addTarget:self action:@selector(createStategyBtn:) forControlEvents:UIControlEventTouchUpInside];
//    _pAddOwnBtn.backgroundColor = kMYHexToRGB(0x999999, 1);
//    [_pAddOwnBtn setTitle:@"加自选" forState:UIControlStateNormal];
//    [_pAddOwnBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [_pAddOwnBtn addTarget:self action:@selector(addOwnBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    _pSellBtn.backgroundColor = kMYHexToRGB(0x00, 1);
    [_pSellBtn setTitle:@"融券卖出" forState:UIControlStateNormal];
    [_pSellBtn setTitleColor:kGREENColor forState:UIControlStateNormal];
    [_pSellBtn addTarget:self action:@selector(createStategyBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    [self addSubview:_pBuyBtn];
//    [self addSubview:_pAddOwnBtn];
    [self addSubview:_pSellBtn];
    
    [self p_initConstraints];
}

- (void)p_initConstraints
{
    if ([HZUserManager manager].isNormalShow) {
//        [_pBuyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.left.bottom.mas_equalTo(self);
//            make.right.mas_equalTo(_pAddOwnBtn.mas_left);
//        }];
//        [_pAddOwnBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.bottom.mas_equalTo(self);
//            make.right.mas_equalTo(_pSellBtn.mas_left);
//        }];
//        [_pSellBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.right.bottom.mas_equalTo(self);
//            make.width.mas_equalTo(@[_pBuyBtn, _pAddOwnBtn]);
//        }];
//        [_pAddOwnBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.left.right.bottom.mas_equalTo(self);
//        }];
    } else {
//        [_pAddOwnBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.left.right.bottom.mas_equalTo(self);
//        }];
    }
}
#pragma mark -
#pragma mark public
- (void)bindCodeInfo:(HZOwnedStockModel *)codeInfo
{
    _codeStr = codeInfo.model_Code;
    _marketStr = [NSString stringWithFormat:@"%d",codeInfo.nModel_Market];
    if ([HZNetworkTool networkIsOwnStock:codeInfo.model_Code WithMarketId:codeInfo.nModel_Market]) {
//        [_pAddOwnBtn setTitle:@"删自选" forState:UIControlStateNormal];
    }else {
//        [_pAddOwnBtn setTitle:@"加自选" forState:UIControlStateNormal];
    }
    if (codeInfo.nModel_Style == 1) {
        _pBuyBtn.enabled = _pSellBtn.enabled = NO;
        [_pBuyBtn setTitle:@"" forState:UIControlStateNormal];
        [_pSellBtn setTitle:@"" forState:UIControlStateNormal];
    } else {
        _pBuyBtn.enabled = _pSellBtn.enabled = YES;
        [_pBuyBtn setTitle:@"买入" forState:UIControlStateNormal];
        [_pSellBtn setTitle:@"融券卖出" forState:UIControlStateNormal];
    }
}

#pragma mark -
#pragma mark action
- (void)addOwnBtn:(UIButton *)btn
{
    if([btn.currentTitle isEqualToString:@"加自选"]){
        [btn setTitle:@"删自选" forState:UIControlStateNormal];
//        [ZQHttpManager addLocalCodeNetWork:_codeStr andMarkrt:_marketStr andSuccess:^(NSDictionary * _Nonnull reslutDic) {
//
//        } andFailed:^(NSError * _Nullable error, NSDictionary * _Nullable resultDic) {
//            [CBToast showToastAction:@"加自选失败!"];
//        }];
    }else{
        [btn setTitle:@"加自选" forState:UIControlStateNormal];
//        [ZQHttpManager deleteLocalCodeNetWork:_codeStr andMarkrt:_marketStr andSuccess:^(NSDictionary * _Nonnull reslutDic) {
//            
//        } andFailed:^(NSError * _Nullable error, NSDictionary * _Nullable resultDic) {
//            [CBToast showToastAction:@"删自选失败!"];
//        }];
    }
//    LogicRouterBlock block = [self.logicManager matchLogicBlock:kLogicRouterAddOwn];
//    NSNumber *result = block(nil, [btn currentTitle]);
//    if ([btn.currentTitle isEqualToString:@"加自选"] && [result intValue] == 0) {
//        [btn setTitle:@"删自选" forState:UIControlStateNormal];
//    }else if ([btn.currentTitle isEqualToString:@"删自选"] && [result intValue] == 0) {
//        [btn setTitle:@"加自选" forState:UIControlStateNormal];
//    }
}
- (void)createStategyBtn:(UIButton *)btn
{
    NSString *bsType = @"";
    if ([btn isEqual:_pBuyBtn]) {
        bsType = @"0";
    } else if ([btn isEqual:_pSellBtn]) {
        bsType = @"1";
    }
    LogicRouterBlock block = [self.logicManager matchLogicBlock:kLogicRouterCreateStategy];
    block(nil, bsType);

}

@end
